<?php
namespace App\Http\Controllers;

use App\Models\Admin;
use App\Models\Course;
use App\Models\CourseMemberAccess;
use App\Models\Member;
use App\Models\Role;
use Illuminate\Support\Facades\DB;
use Input, Auth, Validator;
use App\Models\Model;
use Maatwebsite\Excel\Facades\Excel;

class MemberController extends RestfulBaseController
{
    protected $modelName = 'App\Models\Member';

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $inputs = Input::all();
        $pager = Member::getMemberByTag($inputs);
        return view('admin.member.list', compact('pager'));
    }

    public function show()
    {
        $roles = Role::all();
        $course = Course::getUserByTag();
        return view('admin.member.item', compact('roles', 'course'));
    }

    public function edit($id)
    {
        $tag = Admin::tag();
        $course = Course::getUserByTag();
        $access = CourseMemberAccess::select("course_id")->where(['user_id' => $id])->get()->toArray();
        $accessArr = array_column($access, 'course_id');
        $user = new Member();
        $item = $user->find($id);
        if($tag !== 'customer'){
            return view('admin.member.edit', compact('roles', 'item', 'accessArr', 'course'));
        }else{
            return view('admin.member.item', compact('roles', 'item', 'accessArr', 'course'));
        }

    }


    protected function _updateModel(Model $user, Array $inputs, $type)
    {
        $username = Input::get('username');
        //判断用户名字是否重复
        if (!Input::get('id'))
        {
            $member = Member::where(['username' => $username])->get();
            if ($member->first())
            {
                return $this->retJson(504, '帐号已经存在!');
            }
        }
        //判断用户是否存在
        $user->username = $username ? $username : date("ym") . date('His');
        $user->nickname = Input::get('nickname');
        $user->password = Input::get('password') ? Input::get('password') : 8888;
        $user->mobile = Input::get('mobile');
        $user->sex = Input::get('sex', '1');
        //新增用户所有权限
        $perms = Input::get('perms', []);
        if (empty($perms))
        {
            return $this->retJson(503, '请至少选择一个权限!');
        }
        DB::beginTransaction();
        if (!$user->save())
        {
            return $this->retJson(503, '操作出错!');
        } else
        {
            //删除所有关系
            CourseMemberAccess::where('user_id', '=', $user->id)->delete();
            foreach ($perms as $item)
            {
                $access = new CourseMemberAccess();
                $access->user_id = $user->id;
                $access->course_id = $item;
                $access->save();
            }
        }
        DB::commit();
        return $this->retJson(200, '操作成功!');
    }

    public function destroy($id)
    {

        $member = new Member();
        $member->id = $id;
        $member->exists = true;
        if (!$member->delete())
        {
            return $this->retJson(503, '操作出错!');
        }
        $this->appOpLog("更新数据ID:$id", $member->getAttributes());
        return $this->retJson(200, '操作成功!');
    }
    public function look(){
        $inputs = Input::all();
        $pager = Member::getMemberByTag($inputs);
        return view('admin.member.look', compact('pager'));
    }
    public function export()
    {
        ini_set('memory_limit', '500M');
        set_time_limit(0);//设置超时限制为0分钟
        $cellData = Member::select('username', 'password', 'nickname')->limit(5)->get()->toArray();
        $cellData[0] = array('帐号', '密码', '昵称');
        for ($i = 0; $i < count($cellData); $i++)
        {
            $cellData[$i] = array_values($cellData[$i]);
            $cellData[$i][0] = str_replace('=', ' ' . '=', $cellData[$i][0]);
        }
        Excel::create('用户信息', function ($excel) use ($cellData)
        {
            $excel->sheet('score', function ($sheet) use ($cellData)
            {
                $sheet->rows($cellData);
            });
        })->export('xls');
        die;
    }

    public function import()
    {
//        $filePath = 'upload/user.xls';
//        Excel::load($filePath, function ($reader){
////            $data = $reader->all();
////            dd($data);
//        });
//        $filePath = 'storage/exports/' . iconv('UTF-8', 'GBK', '用户信息') . '.xls';
//        Excel::load($filePath, function ($reader)
//        {
//            $data = $reader->all();
//            dd($data);
//        });
    }
}
